package ai.restosuite.infrastructure.action;

import ai.restosuite.infrastructure.i18n.I18n;
import ai.restosuite.infrastructure.i18n.I18nKey;
import com.intellij.ide.BrowserUtil;
import com.intellij.ide.util.PropertiesComponent;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.util.IconLoader;
import com.intellij.util.ReflectionUtil;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;

import java.util.Objects;

/**
 * 帮助
 *
 * @author yuhao.wang3
 */
public class HelpAction extends AnAction {
    public HelpAction() {
        super("打印日志", "打印日志", IconLoader.getIcon("/icons/help.svg", Objects.requireNonNull(ReflectionUtil.getGrandCallerClass())));
    }

    @Override
    public void update(@NotNull AnActionEvent event) {
        super.update(event);
        I18n.init(event.getProject());
        PropertiesComponent component = PropertiesComponent.getInstance(event.getProject());
        String logSwitch = component.getValue("LOG_" +  event.getProject().getName() + "_SWITCH");
        event.getPresentation().setText(StringUtils.equalsIgnoreCase(logSwitch, "true") ? I18n.getContent(I18nKey.HELP_CLOSE_ACTION$TEXT) : I18n.getContent(I18nKey.HELP_ACTION$TEXT));
    }

    @Override
    public void actionPerformed(AnActionEvent event) {
        PropertiesComponent component = PropertiesComponent.getInstance(event.getProject());
        String value = StringUtils.equalsIgnoreCase(event.getPresentation().getText(), I18n.getContent(I18nKey.HELP_ACTION$TEXT))? "true" : "false";
        component.setValue("LOG_" +  event.getProject().getName() + "_SWITCH", value);
    }
}
